#include <iostream>
#include <sw/redis++/redis++.h>
#include <vector>
#include <thread>
#include "util.hpp"
using sw::redis::Redis;

void test1(Redis& redis)
{
    std::cout << "get 和 set" << std::endl;
    redis.flushall();

    redis.set("key","1");
    redis.set("key1","2");
    redis.set("key3","3");
    auto value = redis.get("key3");
    if(value)
        std::cout << value.value() << std::endl;
}
void test2(Redis& redis)
{
    using namespace std::chrono_literals;
    std::cout << "set 带有超时时间" << std::endl;
    redis.flushall();
    redis.set("key","1",10s);
    std::this_thread::sleep_for(3s);

    long long time = redis.ttl("key");
    std::cout << "time: " << time << std::endl;
}
void test3(Redis& redis)
{
    using namespace std::chrono_literals;
    std::cout << "set NX 和 XX" << std::endl;
    redis.flushall();

    redis.set("key","222",0s,sw::redis::UpdateType::NOT_EXIST);

    auto value = redis.get("key");
    if (value) 
    {
        std::cout << "value: " << value.value() << std::endl;
    } 
    else 
    {
        std::cout << "key 不存在!" << std::endl;
    }
}
void test4(Redis& redis)
{
    std::cout << "mset" << std::endl;

    redis.flushall();
    redis.mset({std::make_pair("key1","1"),std::make_pair("key2","2")});
    
    std::vector<std::pair<std::string, std::string>> keys = {
        {"key3", "111"},
        {"key4", "222"},
        {"key5", "333"}
    };
    redis.mset(keys.begin(),keys.end());
    auto value = redis.get("key1");
    if (value) 
    {
        std::cout << "value: " << value.value() << std::endl;
    }

    value = redis.get("key2");
    if (value) 
    {
        std::cout << "value: " << value.value() << std::endl;
    }

    value = redis.get("key3");
    if (value) 
    {
        std::cout << "value: " << value.value() << std::endl;
    }
}
void test5(Redis& redis)
{
    std::cout << "mget" << std::endl;
    redis.flushall();
    std::vector<std::pair<std::string, std::string>> keys = {
        {"key1", "111"},
        {"key2", "222"},
        {"key3", "333"}
    };
    redis.mset(keys.begin(), keys.end());

    std::vector<sw::redis::OptionalString> result;
    auto it = std::back_inserter(result);
    redis.mget({"key1", "key2", "key3", "key4"}, it);

    printContainerOptional(result);
}
int main()
{
    Redis redis("tcp://127.0.0.1:6379");
    //test1(redis);
    //test2(redis);
    //test3(redis);
    //test4(redis);
    test5(redis);
}